home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / Apps / Clocks / aa_m68k_Only / Alarm / Source / AlarmClock.m < prev    next >
Encoding:
Text File  |  1993-05-31  |  6.8 KB  |  248 lines

  1.  
  2. #import "AlarmClock.h"
  3. #import "Animator.h"
  4.  
  5. // Some notes:  there are three alarms.  You can set any or
  6. //        all of the three alarms any way you like.  The alarm only looks at
  7. //        minutes and hours; it ignores seconds...and only has an accuracy
  8. //        of +/- 5 sec. with respect to the system clock, since I only check
  9. //        every ten seconds to see if it's time to go off.  A better way to
  10. //        do this might be to have three timed entries, each set to go off
  11. //        at the right time and call the proper alarm trigger function, but
  12. //        this way works just fine for me.  I really need to implement prefs
  13. //        especially for the "x minutes after launch" alarm:  that would be
  14. //        nice to suggest that you to log off after a certain amount of time.
  15. //        Also, the interface is a bit confusing and very ugly...
  16.  
  17. @implementation AlarmClock
  18.  
  19. - appDidInit:sender
  20. {
  21.     int i;
  22.     launchTime = [[DAYTime alloc] initWithCurrentTime];
  23.     fromSet = [[DAYTime alloc] init];
  24.     absolute = [[DAYTime alloc] init];
  25.     fromLaunch = [[DAYTime alloc] init];
  26.     for (i=0; i<3; i++) {
  27.         alarms[i] = [[DAYTime alloc] initWithCurrentTime];
  28.     }
  29.     // set up a timer to call us once every so often.
  30.     animator = [[Animator alloc] initChronon:ALARM_FREQUENCY
  31.             adaptation:0.0   target:self
  32.             action:@selector(tick:) autoStart:YES eventMask:0];
  33.     [self doAtSetTime:self];
  34.     return self;
  35. }
  36.  
  37. - setAlarm:sender
  38. {
  39.     if (alarmType[num] < 0) {
  40.         NXRunAlertPanel(NULL, [strings valueForStringKey:"setTfirst"],
  41.                 [strings valueForStringKey:"OK"], NULL, NULL);
  42.         return nil;
  43.     }
  44.     if (alarms[num]) [alarms[num] free];
  45.     switch (num) {
  46.         case 0 : {
  47.             [absolute setMinute:[[timeMatrix findCellWithTag:1] intValue]];
  48.             [absolute setHour:[[timeMatrix findCellWithTag:0] intValue]];
  49.             alarms[0] = [absolute copy];
  50.             break;
  51.         }
  52.         case 1 : {
  53.             [fromSet setMinute:[[timeMatrix findCellWithTag:1] intValue]];
  54.             [fromSet setHour:[[timeMatrix findCellWithTag:0] intValue]];
  55.             alarms[1] = [[DAYTime alloc] initWithCurrentTime];
  56.             // the addTime: method isn't done yet, so I'm using these.
  57.             [alarms[1] addMinutes:[fromSet minute]];
  58.             [alarms[1] addHours:[fromSet hour]];
  59.             break;
  60.         }
  61.         case 2 : {
  62.             [fromLaunch setMinute:[[timeMatrix findCellWithTag:1] intValue]];
  63.             [fromLaunch setHour:[[timeMatrix findCellWithTag:0] intValue]];
  64.             alarms[2] = [launchTime copy];
  65.             [alarms[2] addMinutes:[fromLaunch minute]];
  66.             [alarms[2] addHours:[fromLaunch hour]];
  67.             break;
  68.         }
  69.         default: break;    // should never get here
  70.     }
  71.     alarmOn[num] = YES;
  72.     return self;
  73. }
  74.  
  75. - tick:sender
  76. {
  77.     int i;
  78.     DAYTime *currentTime = [[DAYTime alloc] initWithCurrentTime];
  79.  
  80.     for (i=0; i<3; i++) {
  81.         if (alarmOn[i] &&
  82.                 ([currentTime minute]  == [alarms[i] minute]) &&
  83.                 ([currentTime hour]  == [alarms[i] hour])) {
  84.             // time to fire off the alarm
  85.             if (((alarmType[i] == ALARM_SOUND_AND_GRAPHIC) ||
  86.                     (alarmType[i] == ALARM_GRAPHIC)) && images[i]) {
  87.                 // put up a graphic
  88.                 NXRect theFrame; NXSize theSize;
  89.                 NXPoint aPoint = {0, 0}; Window *aWindow;
  90.                 [images[i] getSize:&theSize];
  91.                 // really, I should grab the actual screen size here
  92.                 // but for now I'm using the standard NeXT screen size
  93.                 // (1120x832) since I don't have time to do it right
  94.                 NXSetRect(&theFrame, (1120-theSize.width-2)/2,
  95.                         (832-theSize.height-21)/2,
  96.                         theSize.width, theSize.height);
  97.                 aWindow = [[Window alloc] initContent:&theFrame
  98.                         style:NX_TITLEDSTYLE backing:NX_RETAINED
  99.                         buttonMask:NX_CLOSEBUTTONMASK defer:NO];
  100.                 [aWindow setTitle:
  101.                         [strings valueForStringKey:"ImageWindowTitle"]];
  102.                 [aWindow display];
  103.                 [[aWindow contentView] lockFocus];
  104.                 [images[i] composite:NX_COPY toPoint:&aPoint];
  105.                 [[aWindow contentView] unlockFocus];
  106.                 [aWindow makeKeyAndOrderFront:self];
  107.                 [NXApp activateSelf:YES];
  108.             }
  109.             if ((alarmType[i] == ALARM_SOUND_AND_GRAPHIC) ||
  110.                     (alarmType[i] == ALARM_SOUND)) [sounds[i] play];
  111.             if (alarmType[i] == ALARM_UNIX_COMMAND) {
  112.                 // do a UNIX command
  113.                 system([commands[i] stringValue]);
  114.             }
  115.             // make sure the alarm doesn't go off more than once
  116.             alarmOn[i] = NO;
  117.         }
  118.     }
  119.     return self;
  120. }
  121.  
  122. - free
  123. {
  124.     int i;
  125.     for (i=0; i<3; i++) {
  126.         [alarms[i] free];
  127.         [images[i] free];
  128.         [sounds[i] free];
  129.         [commands[i] free];
  130.     }
  131.     [fromSet free];
  132.     [absolute free];
  133.     [fromLaunch free];
  134.     [launchTime free];
  135.     [super free];
  136.     return self;
  137. }
  138.  
  139. - getSound:sender
  140. {
  141.     const char *const types[] = {"snd", NULL};  
  142.  
  143.     if ([[OpenPanel new] runModalForTypes:types]) {
  144.         [soundText setStringValue:[[OpenPanel new] filename]];
  145.     }
  146.     return self;
  147. }
  148.  
  149. - getImage:sender
  150. {
  151.     const char *const types[] = {"tiff", "eps", NULL};  
  152.  
  153.     if ([[OpenPanel new] runModalForTypes:types]) {
  154.         [imageText setStringValue:[[OpenPanel new] filename]];
  155.     }
  156.     return self;
  157. }
  158.  
  159. - setSound:sender
  160. {
  161.     if (sounds[num]) [sounds[num] free];
  162.     sounds[num] = [[Sound alloc] initFromSoundfile:[soundText stringValue]];
  163.     if (!sounds[num]) {
  164.         NXRunAlertPanel(NULL, [strings valueForStringKey:"setSfirst"],
  165.                 [strings valueForStringKey:"OK"], NULL, NULL);
  166.         return nil;
  167.     }
  168.     alarmType[num] = ALARM_SOUND;
  169.     return self;
  170. }
  171.  
  172. - setGraphic:sender
  173. {
  174.     if (images[num]) [images[num] free];
  175.     images[num] = [[NXImage alloc] initFromFile:[imageText stringValue]];
  176.     if (!images[num]) {
  177.         NXRunAlertPanel(NULL, [strings valueForStringKey:"setGfirst"],
  178.                 [strings valueForStringKey:"OK"], NULL, NULL);
  179.         return nil;
  180.     }
  181.     alarmType[num] = ALARM_GRAPHIC;
  182.     return self;
  183. }
  184.  
  185. - setSoundAndGraphic:sender
  186. {
  187.     if (!sounds[num])
  188.         sounds[num] =
  189.             [[Sound alloc] initFromSoundfile:[soundText stringValue]];
  190.     if (!images[num])
  191.         images[num] = [[NXImage alloc] initFromFile:[imageText stringValue]];
  192.     if (!sounds[num] && !images[num]) {
  193.         NXRunAlertPanel(NULL, [strings valueForStringKey:"setSandGfirst"],
  194.                 [strings valueForStringKey:"OK"], NULL, NULL);
  195.         return nil;
  196.     }
  197.     alarmType[num] = ALARM_SOUND_AND_GRAPHIC;
  198.     return self;
  199. }
  200.  
  201. - setUnixCommand:sender
  202. {
  203.     [NXApp runModalFor:unixWindow]; // get the command
  204.     alarmType[num] = ALARM_UNIX_COMMAND;
  205.     return self;
  206. }
  207.  
  208. - gotUnixCommand:sender
  209. {
  210.     if (commands[num]) [commands[num] free];
  211.     commands[num] = [[DAYString alloc] initString:[unixText stringValue]];
  212.     [unixWindow orderOut:self];
  213.     [NXApp stopModal];
  214.     return self;
  215. }
  216.  
  217. - doAtSetTime:sender
  218. {
  219.     num = 0;
  220.     [typeList setTitle:[strings valueForStringKey:"Sound"]];
  221.     alarmType[num] = -1;
  222.     [[timeMatrix findCellWithTag:1] setIntValue:[absolute minute]];
  223.     [[timeMatrix findCellWithTag:0] setIntValue:[absolute hour]];
  224.     return self;
  225. }
  226.  
  227. - doFromNow:sender
  228. {
  229.     num = 1;
  230.     [typeList setTitle:[strings valueForStringKey:"Sound"]];
  231.     alarmType[num] = -1;
  232.     [[timeMatrix findCellWithTag:1] setIntValue:[fromSet minute]];
  233.     [[timeMatrix findCellWithTag:0] setIntValue:[fromSet hour]];
  234.     return self;
  235. }
  236.  
  237. - doFromLaunch:sender
  238. {
  239.     num = 2;
  240.     [typeList setTitle:[strings valueForStringKey:"Sound"]];
  241.     alarmType[num] = -1;
  242.     [[timeMatrix findCellWithTag:1] setIntValue:[fromLaunch minute]];
  243.     [[timeMatrix findCellWithTag:0] setIntValue:[fromLaunch hour]];
  244.     return self;
  245. }
  246.  
  247. @end
  248.